package mybatis.generator;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class Generator {

    @Test
    public void genCode(){

        String prefix = "anjo_";

        String modulName = "user";

        //1、创建代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        //2、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath + "/src/main/java");
        globalConfig.setAuthor("playmaker");
        globalConfig.setOpen(false); //不打开资源管理器
        globalConfig.setFileOverride(false); //重新生成时不覆盖
        globalConfig.setServiceName("%sService"); //去掉Service接口的首字母I
        globalConfig.setIdType(IdType.AUTO); //主键自增
        globalConfig.setDateType(DateType.TIME_PACK); //生成实体类中日期类型
        globalConfig.setSwagger2(true);
        autoGenerator.setGlobalConfig(globalConfig);


        //3、数据源
        DataSourceConfig ds = new DataSourceConfig();
        ds.setUrl("jdbc:mysql://cdb-1j8254u8.cd.tencentcdb.com:10186/" + prefix + modulName + "?useUnicode=true&useSSL=false&characterEncoding=utf8");
        ds.setDriverName("com.mysql.jdbc.Driver");
        ds.setUsername("root");
        ds.setPassword("15362941637@aab!");
        ds.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(ds);


        //4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("member"); //模块名称
        pc.setParent("com.zsc.wxm.ysh.anjo");
        pc.setController("controller");
        pc.setService("service");
        pc.setEntity("model");
        pc.setMapper("mapper");
        autoGenerator.setPackageInfo(pc);

        //5、策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel); //数据库表映射到实体的命名策略
        strategyConfig.setTablePrefix(modulName + "_"); //设置表前缀不生成
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); //数据库表映射到实体的命名策略
        strategyConfig.setEntityLombokModel(true);

        strategyConfig.setLogicDeleteFieldName("is_deleted");
        strategyConfig.setEntityBooleanColumnRemoveIsPrefix(true);


        //自动填充
        TableFill create = new TableFill("create_time", FieldFill.INSERT);
        TableFill modify = new TableFill("modify_time", FieldFill.INSERT_UPDATE);

        List<TableFill> tableFills = new ArrayList<>();
        tableFills.add(create);
        tableFills.add(modify);
        strategyConfig.setTableFillList(tableFills);

        strategyConfig.setRestControllerStyle(true); //restful api 风格
        strategyConfig.setControllerMappingHyphenStyle(true); //url转链接字符-


        //设置BaseModel
        strategyConfig.setSuperEntityClass("com.zsc.wxm.ysh.anjo.service.base.model.BaseModel");

        // 填写BaseEntity中的公共字段
        strategyConfig.setSuperEntityColumns("id", "create_time", "modify_time","version");
        autoGenerator.setStrategy(strategyConfig);

        // 6、执行
        autoGenerator.execute();




    }
}
